'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { INTEGER, DATE, STRING } = app.Sequelize;
    const PostJoin = app.model.define(
      'PostJoin',
          {
              id: {type: INTEGER, primaryKey: true, autoIncrement: true},
              user_id: INTEGER,
              category_id: INTEGER,
              category: STRING(255),
              post_id: INTEGER,
              is_read: INTEGER,
              resume_id: INTEGER,
              company_user_id: INTEGER,
              create_time: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                  }
              },
              updated_time: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                  }
              }
          },
          {
              tableName: 'post_join',
              timestamps: true, // 启用时间戳(createdAt, updatedAt)
              updatedAt: 'updated_time', // 不想要 updatedAt
              createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 create_time
          }
    );
    PostJoin.associate = function() {
        // 投递者
        app.model.PostJoin.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id', as: 'user' });
        app.model.PostJoin.belongsTo(app.model.Resume, { foreignKey: 'resume_id', targetKey: 'id', as: 'resume' });
        // 岗位
        app.model.PostJoin.belongsTo(app.model.Post, { foreignKey: 'post_id', targetKey: 'id', as: 'post' });
        // 公司
        app.model.PostJoin.belongsTo(app.model.User, { foreignKey: 'company_user_id', targetKey: 'id', as: 'company' })
    }
    return PostJoin;
};

